package com.qiaoyanqing.ximalaya.discovermodule.discover_adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_ClassifySecondBookSuspenseEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by hp on 2016/12/13.
 */

public class Discover_ClassifySecondLVAdapter extends BaseAdapter {

    private Context context;
    private List<Discover_ClassifySecondBookSuspenseEntity.SuspenseListEntity> dataList;
    private LayoutInflater inflater;

    public Discover_ClassifySecondLVAdapter(Context context, List<Discover_ClassifySecondBookSuspenseEntity.SuspenseListEntity> dataList) {
        this.context = context;
        this.dataList = dataList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        BaseViewHolder baseViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.discover_classifysecondbaselist_item, viewGroup, false);
            baseViewHolder = new BaseViewHolder(convertView);
            convertView.setTag(baseViewHolder);
        } else {
            baseViewHolder = (BaseViewHolder) convertView.getTag();
        }
        baseViewHolder.tv_bookTitle.setText(dataList.get(position).getTitle());
        baseViewHolder.tv_intro.setText(dataList.get(position).getIntro());
        baseViewHolder.tv_playsCounts.setText(dataList.get(position).getPlaysCounts()+"");
        baseViewHolder.tv_tracks.setText(dataList.get(position).getTracks()+"");

        if (dataList.get(position).getCoverSmall() != null){
            Picasso.with(context).load(dataList.get(position).getCoverSmall()).into(baseViewHolder.iv_bookCover);
        }
        return convertView;
    }

    private static class BaseViewHolder {
        ImageView iv_bookCover;
        TextView tv_bookTitle,tv_intro,tv_playsCounts,tv_tracks;

        public BaseViewHolder(View convertView) {
            iv_bookCover = (ImageView) convertView.findViewById(R.id.iv_classify_second_book_cover);
            tv_bookTitle = (TextView) convertView.findViewById(R.id.tv_classify_second_book_title);
            tv_intro = (TextView) convertView.findViewById(R.id.tv_classify_second_book_intro);
            tv_playsCounts = (TextView) convertView.findViewById(R.id.tv_classify_second_book_playsCounts);
            tv_tracks = (TextView) convertView.findViewById(R.id.classify_second_book_tracks);
        }
    }
}
